package com.rxyb.data.consumer;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.rxyb.data.db.po.RecordReqLog;
import com.rxyb.data.service.logic.RecordReqLogLogic;
import com.rxyb.rabbit.dto.EventDTO;
import com.rxyb.rabbit.dto.LogDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.rxyb.rabbit.constants.EventConstant.MY_LOG;

/**
 * @author YJH
 * @version 1.0
 * @description 日志记录
 * @date 2020/6/19 18:12
 */
@Component
@Slf4j
public class LogCustomer {


    private final RecordReqLogLogic recordReqLogLogic;

    public LogCustomer(RecordReqLogLogic recordReqLogLogic) {
        this.recordReqLogLogic = recordReqLogLogic;
    }

    @RabbitListener(queues = MY_LOG)
    public void customer(EventDTO dto) {
        log.info("log rabbitmq ->dto:[{}]", JSONUtil.toJsonStr(dto));
        RecordReqLog reqLog = new RecordReqLog()
                .setReqContent(dto.getContext())
                .setMethod(JSONUtil.toBean(dto.getContext(), LogDTO.class).getRequestMethod())
                .setType(dto.getType());
        recordReqLogLogic.add(reqLog);
    }
}
